package com.jeesite.modules.general.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.general.service.GeneralService;
import com.jeesite.modules.general.utils.CosApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Mr Wu
 * @date 2025/10/29
 */
@Controller
@RequestMapping(value = "${adminPath}/general")
public class GeneralController extends BaseController {

    @Autowired
    private GeneralService generalService;

    /**
     * 商家申请加入
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "applyJoin")
    public String applyJoin(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        String rowId = request.getParameter("id");
        if(StringUtils.isNotEmpty(rowId)){
            String license = request.getParameter("delLicense");
            if (ObjectUtils.isNotEmpty(license)) {
                CosApiUtils.delCOSFile(license,"license");
            }
        }
        Map map = new HashMap();
        map.put("id", StringUtils.isNotEmpty(rowId)?rowId:IdGen.nextId());
        map.put("name", request.getParameter("name"));
        map.put("place", request.getParameter("place"));
        map.put("director", request.getParameter("director"));
        map.put("phone", request.getParameter("phone"));
        map.put("createBy", request.getParameter("createBy"));
        map.put("reviewStatus", "0");
        String filename = file.getOriginalFilename();
        if(!"all.png".equals(filename)){
            JSONObject files = CosApiUtils.uploadFile(file,map.get("id")+".png","license");
            map.put("licenseUrl", files.getString("fileName2"));
        }
        if(StringUtils.isNotEmpty(rowId)){
            generalService.updateApplyJoin(map);
        }else {
            generalService.saveApplyJoin(map);
        }
        return renderResult(Global.TRUE, text("身份核验成功！"), map.get("id"));
    }
    /**
     * 上传商家logo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "uploadLogo")
    public String uploadLogo(@RequestParam(value = "file") MultipartFile file, String id, String delLogo){
        if (ObjectUtils.isNotEmpty(delLogo)) {
            CosApiUtils.delCOSFile(delLogo,"license");
        }
        JSONObject files = CosApiUtils.uploadFile(file,id+".png","logo");
        Map map = new HashMap();
        map.put("id", id);
        map.put("logoUrl", files.getString("fileName2"));
        generalService.updateApplyJoin(map);
        return renderResult(Global.TRUE, text("上传商家logo成功！"));
    }

    /**
     * 获取商家入驻信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getApplyJoin")
    public String getApplyJoin(HttpServletRequest request){
        String memberId = request.getParameter("memberId");
        String type = request.getParameter("type");
        Map applyJoin = generalService.getApplyJoin(memberId, null);
        if (ObjectUtils.isEmpty(applyJoin)) {
            return renderResult(Global.FALSE, text(""));
        }
        if(StringUtils.isEmpty(type)){
            applyJoin.put("license",CosApiUtils.URL+applyJoin.get("license"));
            applyJoin.put("logo",CosApiUtils.URL+applyJoin.get("logo"));
        }
        return renderResult(Global.TRUE, text(""), applyJoin);
    }

    /**
     * 关注车辆
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "followCar")
    public String followCar(HttpServletRequest request){
        Map map = new HashMap();
        map.put("followId", IdGen.nextId());
        map.put("carId", request.getParameter("carId"));
        map.put("memberId", request.getParameter("memberId"));
        map.put("isFollow", request.getParameter("isFollow"));
        generalService.followCar(map);
        return renderResult(Global.TRUE, text("关注车辆成功！"));
    }

    /**
     * 浏览记录保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "browseRecord")
    public String browseRecord(HttpServletRequest request){
        String carId = request.getParameter("carId");
        String memberId = request.getParameter("memberId");
        Map map = new HashMap();
        map.put("recordId", IdGen.nextId());
        map.put("carId", carId);
        map.put("memberId", memberId);
        generalService.browseRecord(map, memberId);
        return renderResult(Global.TRUE, text("浏览记录保存成功！"));
    }

    /**
     * 浏览数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findBrowseList")
    public Page<Map> findBrowseList(HttpServletRequest request, HttpServletResponse response){
        Map map = new HashMap();
        map.put("memberId", request.getParameter("memberId"));
        map.put("page", new Page<>(request, response));
        Page<Map> page = generalService.findBrowseList(map);
        return page;
    }

    /**
     * 关注数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findFollowList")
    public Page<Map> findFollowList(HttpServletRequest request, HttpServletResponse response){
        Map map = new HashMap();
        map.put("memberId", request.getParameter("memberId"));
        map.put("page", new Page<>(request, response));
        Page<Map> page = generalService.findFollowList(map);
        return page;
    }

    /**
     * 商家排名
     */
    @ResponseBody
    @RequestMapping(value = "merchantRank")
    public Page<Map> merchantRank(HttpServletRequest request, HttpServletResponse response){
        return generalService.merchantRank(new Page<>(request, response));
    }

    /**
     * 轮播图列表
     */
    @ResponseBody
    @RequestMapping(value = "findCarouselList")
    public String findCarouselList(HttpServletRequest request){
        List<Map> carouselList = generalService.findCarouselList();
        return renderResult(Global.TRUE, text("浏览记录保存成功！"), carouselList);
    }

    /**
     * 地区列表
     */
    @ResponseBody
    @RequestMapping(value = "findCityList")
    public String findCityList(){
        Map<Integer, List<Map<String, String>>> list = generalService.findCityList();
        return renderResult(Global.TRUE, text("success"), list);
    }

}
